package com.lanou.fooddemo.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.lanou.fooddemo.R;
import com.lanou.fooddemo.bean.DataBean;
import com.lanou.fooddemo.external.smart_image_view.SmartImageView;
import com.lanou.fooddemo.fragment.SameCityFragment;
import com.lanou.fooddemo.util.NetUtil;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;

import java.util.List;

public class SameCityInformationActivity extends Activity implements View.OnClickListener{
    private final String LOG_TAG = "SCInfoActivity";
    /**
     * 返回按钮
     */
    private TextView backTextView;
    /**
     * 姓名TextView
     */
    private TextView userNameTextView;
    /**
     * 签名TextView
     */
    private TextView userSignTextView;
    /**
     * 头像ImageView
     */
    private SmartImageView userIcImageView;
    /**
     * 食物ImageView
     */
    private SmartImageView foodImageView;
    /**
     * 内容简介TextView
     */
    private TextView contentTextView;
    /**
     * 店铺图片ImageView
     */
    private SmartImageView shopImageView;
    /**
     * 店铺名字TextView
     */
    private TextView shopNameTextView;
    /**
     * 店铺副标题TextView
     */
    private TextView shopSubTitleTextView;
    /**
     * 店铺内容TextView
     */
    private TextView shopContentTextView;

    private final int DATA = 2;

    /**
     * 数据List
     */
    private List<DataBean> dataBeans;
    /**
     * 用户Bean
     */
    private DataBean userDataBean;
    /**
     * 食物Bean
     */
    private DataBean foodDataBean;
    /**
     * 简介内容Bean
     */
    private DataBean contentBean;
    /**
     * 店铺内容Bean
     */
    private DataBean shopDataBean;

    private Handler handler;
    /**
     * 点击监听接口声明
     */
    private View.OnClickListener clickListener;

    // 首先在您的Activity中添加如下成员变量
    final UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share");

    Button shareButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_same_city_information);
        //初始化视图
        initView();
        //获取数据
        getData();


        // 设置分享内容
        mController.setShareContent("友盟社会化组件（SDK）让移动应用快速整合社交分享功能，http://www.umeng.com/social");
        // 设置分享图片, 参数2为图片的url地址
        mController.setShareMedia(new UMImage(this,
                "http://www.baidu.com/img/bdlogo.png"));

        // 配置需要分享的相关平台
        configPlatforms();
        shareButton.setOnClickListener(this);
        addQQZonePlatform();
    }

    /**
     * 初始化视图
     */
    private void initView() {

        shareButton = (Button) findViewById(R.id.shareButton);


        backTextView = (TextView) findViewById(R.id.sameCityActivityBack);
        userNameTextView = (TextView) findViewById(R.id.sameCityActivityUserName);
        userSignTextView = (TextView) findViewById(R.id.sameCityActivityUserSign);
        userIcImageView = (SmartImageView) findViewById(R.id.sameCityActivityUserIcImageView);
        foodImageView = (SmartImageView) findViewById(R.id.sameCityActivityFoodImageView);
        contentTextView = (TextView) findViewById(R.id.sameCityActivityContent);
        shopImageView = (SmartImageView) findViewById(R.id.sameCityActivityShopImage);
        shopNameTextView = (TextView) findViewById(R.id.sameCityActivityShopName);
        shopSubTitleTextView = (TextView) findViewById(R.id.sameCityActivityShopSubTitle);
        shopContentTextView = (TextView) findViewById(R.id.sameCityActivityShopContent);

        //实例化点击监听类
        clickListener = new InformationOnClickListener();

        //设置返回按钮点击监听
        backTextView.setOnClickListener(clickListener);

        //实例化Handler，通过自定义Callback类
        handler = new Handler(new InformationCallback());

    }

    /**
     * 获取数据
     */
    private void getData() {
        //实例化InformationRunnabale类
        InformationRunnabale informationRunnabale = new InformationRunnabale();
        //传入InformationRunnabale实例类实例化Thread
        Thread thread = new Thread(informationRunnabale);
        //开启线程
        thread.start();
    }

    @Override
    public void onClick(View v) {
        mController.openShare(this, false);
    }

    /**
     * handler的Callback参数类
     */
    private class InformationCallback implements Handler.Callback {

        @Override
        public boolean handleMessage(Message msg) {
            //根据消息的不同msg值做出不同处理
            switch (msg.what) {
                //处理姓名信息
                case DATA :
                    //填充姓名
                    userNameTextView.setText(userDataBean.getTitle());
                    //填充签名
                    userSignTextView.setText(userDataBean.getDesc());
                    //填充头像
                    userIcImageView.setImageUrl(userDataBean.getUser().getAvatar(), SmartImageView.TYPE_ROUND);
                    //填充食物图片
                    foodImageView.setImageUrl(foodDataBean.getPic().getUrl());
                    //填充简介内容数据
                    contentTextView.setText(contentBean.getContent());
                    //填充店铺图片
                    shopImageView.setImageUrl(shopDataBean.getPic().getUrl());
                    //填充店铺名字
                    shopNameTextView.setText(shopDataBean.getTitle());
                    //填充店铺副标题
                    shopSubTitleTextView.setText(shopDataBean.getSubTitle());
                    //填充店铺内容
                    shopContentTextView.setText(shopDataBean.getDesc());
                    break;
            }
            return true;
        }
    }

    /**
     * Thread的Runnable参数类
     */
    private class InformationRunnabale implements Runnable {
        /**
         * 记录Bean类型
         */
        String type = null;

        @Override
        public void run() {
            //从网络工具类获取dataBeans
            dataBeans = NetUtil.getDataBeanListDataForSameCityInformation(getIntent().getStringExtra(SameCityFragment.INFORMATION_KEY));
            //如果获取数据不为空
            if (null != dataBeans) {
                //遍历dataBeans
                for(DataBean dataBean : dataBeans) {
                    type = dataBean.getType();
                    //如果是UserCard类型
                    if (DataBean.TYPE_USER_CARD.equals(type)) {
                        //获取到UserCard类型的Bean
                        userDataBean = dataBean;
                    } else if (DataBean.TYPE_IMAGE_CARD.equals(type)) {
                        //获取ImageCard类型的Bean
                        foodDataBean = dataBean;
                    } else if (DataBean.TYPE_CONTENT_CARD.equals(type)) {
                        //获取ContentCard类型的Bean
                        contentBean = dataBean;
                    } else if (DataBean.TYPE_PIC_CARD.equals(type)) {
                        //获取PicCard类型的店铺Bean
                        shopDataBean = dataBean;
                    }
                }
                //通过handler获取消息
                Message msg = handler.obtainMessage();
                //设置消息what值
                msg.what = DATA;
                //发送消息
                handler.sendMessage(msg);
            } else {
                Log.i(LOG_TAG, "data is null");
            }
        }
    }

    /**
     * 点击事件监听类
     */
    private class InformationOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.sameCityActivityBack :
                    //销毁当前Activity
                    finish();
                    break;
            }
        }
    }



    private void addQQZonePlatform() {
        String appId = "1104731183";
        String appKey = "x7k2uH0CxBFf1Xhs";
        QZoneShareContent shareContent = new QZoneShareContent();
        shareContent.setTitle("遇见美食.知道你会来,所以我会等");
        shareContent.setTargetUrl("http://www.boohee.com");
        mController.setShareMedia(shareContent);
        // 添加QZone平台
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(this, appId, appKey);
        qZoneSsoHandler.addToSocialSDK();
    }


    /**
     * 配置分享平台参数</br>
     */
    private void configPlatforms() {

        // 添加QQ、QZone平台
        addQQQZonePlatform();
    }

    private void addQQQZonePlatform() {

        QZoneShareContent shareContent = new QZoneShareContent();
        shareContent.setTitle("hello");
        shareContent.setTargetUrl("http://www.baidu.com");
        mController.setShareMedia(shareContent);

        String appId = "100424468";
        String appKey = "c7394704798a158208a74ab60104f0ba";

        // 添加QZone平台
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(this, "100424468", "55d2f97be0f55a9658002687");
        qZoneSsoHandler.addToSocialSDK();

        //参数1为当前Activity， 参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(this, "100424468",
                "55d2f97be0f55a9658002687");
        qqSsoHandler.addToSocialSDK();
    }

}
